/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package 解析.node.node;

import 解析.Matcher;
import 解析.Pattern;

/**
 *
 * @author yanxi
 */
public class NotBehindS extends NotBehind {

    public NotBehindS(Node cond, int rmax, int rmin) {
        super(cond, rmax, rmin);
    }

    @Override
    public boolean match(Matcher matcher, int i, CharSequence seq) {
        int rmaxChars = Pattern.countChars(seq, i, -rmax);
        int rminChars = Pattern.countChars(seq, i, -rmin);
        int savedFrom = matcher.from;
        int savedLBT = matcher.lookbehindTo;
        boolean conditionMatched = false;
        int startIndex = (!matcher.transparentBounds)
                ? matcher.from : 0;
        int from = Math.max(i - rmaxChars, startIndex);
        matcher.lookbehindTo = i;
        // Relax transparent region boundaries for lookbehind
        if (matcher.transparentBounds) {
            matcher.from = 0;
        }
        for (int j = i - rminChars;
                !conditionMatched && j >= from;
                j -= j > from ? Pattern.countChars(seq, j, -1) : 1) {
            conditionMatched = cond.match(matcher, j, seq);
        }
        //Reinstate region boundaries
        matcher.from = savedFrom;
        matcher.lookbehindTo = savedLBT;
        return !conditionMatched && next.match(matcher, i, seq);
    }
}
